// components/recomment/recomment.js
let api = require("../../utils/api.js")
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    goodsRecommend: {
      type: Array,
      value: []
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    goodsRecommend: []
  },
  /**
   * 生命周期
   */
  lifetimes: {
    ready: function(options) {

    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 商品数量增加
     */
    addNum: function(e) {
      let that = this
      let item = e.currentTarget.dataset.item
      let id = item.id
      let list = this.data.goodsRecommend
      let index = list.findIndex(item => item.id === id)
      let detailStartNum = Number(list[index].detailStartNum)
      let amount = Number(list[index].shopAmout)
      if (list[index].shopAmout ==0){
        amount = detailStartNum
      }else{
        amount = amount * 1 + 1
      }
      
      
       // 访问接口更新数据
       // TODO 获取userId/cityId
      let userInfoId = wx.getStorageSync("openId")
      let cityId = wx.getStorageSync("cityId")
      let goodsName = item.name
      let goodsDetailsId = item.detailId
      let price = item.detailPrice
      let totalPrice = price * amount
      
      api.set_amount({
        cityId,
        userInfoId,
        goodsName,
        goodsDetailsId,
        amount,
        price,
        totalPrice
      }).then(res => {
        if (res.result) {
          list[index].shopAmout=amount
          this.setData({ goodsRecommend: list })
         
            that.triggerEvent('uploadData', {list})
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
         
      })
    },
    /**
     * 商品数量减少
     */
    subNum: function(e) {
      let that = this
      let item = e.currentTarget.dataset.item
      let id = item.id
      let list = this.data.goodsRecommend
      let index = list.findIndex(item => item.id === id)
      let detailStartNum = Number(list[index].detailStartNum)
      let amount = Number(list[index].shopAmout)
      if (amount == detailStartNum){
        amount=0
      }else{
        amount = amount-1
      }

      // 访问接口更新数据

      // TODO 获取userId/cityId
      let userInfoId = wx.getStorageSync("openId")
      let cityId = wx.getStorageSync("cityId")
      let goodsName = item.name
      let goodsDetailsId = item.detailId
      let price = item.detailPrice
      let totalPrice = price * amount
      api.set_amount({
        cityId,
        userInfoId,
        goodsName,
        goodsDetailsId,
        amount,
        price,
        totalPrice
      }).then(res => {
        if (res.result) {
          list[index].shopAmout = amount
          this.setData({ goodsRecommend: list })

          that.triggerEvent('uploadData')

        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }

      })
    },
    // 同步数据
    setGoodsRecommend(list) {
      list.forEach(item => {
        if (!item.shopAmout) {
          item.shopAmout = 0
        }
      })
      let idList = []
      list.filter(item => {
        if (idList.findIndex(i => i.id == item.id) != -1){
          idList.push(item)
        }
      })
      
      this.setData({
        goodsRecommend: idList
      })
    },

    //去详情
    details: function (e) {
      wx.navigateTo({
        url: '../detail/detail?id=' + e.currentTarget.dataset.id,
      })
    }
  }

  
})
